package server.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.demo.dto.ApiResponse;
import server.demo.service.StatisticsService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/statistics")
@CrossOrigin(origins = "*")
public class StatisticsController {
    
    @Autowired
    private StatisticsService statisticsService;
    
    @GetMapping("/overall")
    public ApiResponse<Map<String, Object>> getOverallStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getOverallStatistics();
            return ApiResponse.success("获取统计信息成功", stats);
        } catch (Exception e) {
            return ApiResponse.error("获取统计信息失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/today")
    public ApiResponse<Map<String, Object>> getTodayStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getTodayStatistics();
            return ApiResponse.success("获取今日统计成功", stats);
        } catch (Exception e) {
            return ApiResponse.error("获取今日统计失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/revenue")
    public ApiResponse<Map<String, Object>> getRevenueStatistics() {
        try {
            Map<String, Object> stats = statisticsService.getRevenueStatistics();
            return ApiResponse.success("获取收入统计成功", stats);
        } catch (Exception e) {
            return ApiResponse.error("获取收入统计失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/popular-trains")
    public ApiResponse<List<Object[]>> getPopularTrains(@RequestParam(defaultValue = "10") int limit) {
        try {
            List<Object[]> trains = statisticsService.getPopularTrains(limit);
            return ApiResponse.success("获取热门车次成功", trains);
        } catch (Exception e) {
            return ApiResponse.error("获取热门车次失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/popular-routes")
    public ApiResponse<List<Object[]>> getPopularRoutes(@RequestParam(defaultValue = "10") int limit) {
        try {
            List<Object[]> routes = statisticsService.getPopularRoutes(limit);
            return ApiResponse.success("获取热门线路成功", routes);
        } catch (Exception e) {
            return ApiResponse.error("获取热门线路失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/user/{userId}")
    public ApiResponse<Map<String, Object>> getUserOrderStatistics(@PathVariable Long userId) {
        try {
            Map<String, Object> stats = statisticsService.getUserOrderStatistics(userId);
            return ApiResponse.success("获取用户统计成功", stats);
        } catch (Exception e) {
            return ApiResponse.error("获取用户统计失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/train/{trainId}/availability")
    public ApiResponse<Map<String, Object>> getTrainSeatAvailability(@PathVariable Long trainId) {
        try {
            Map<String, Object> stats = statisticsService.getTrainSeatAvailability(trainId);
            return ApiResponse.success("获取车次座位统计成功", stats);
        } catch (Exception e) {
            return ApiResponse.error("获取车次座位统计失败: " + e.getMessage());
        }
    }
}